const { DataTypes } = require("sequelize");
const sequelize = require("../db");
const User = require("./user.model");
const ApiList = require("./apiList.model");

const ApiListDetails = sequelize.define(
    "ApiListDetails",
    {
        apiListId: {
            type: DataTypes.INTEGER,
            allowNull: false,
            references: {
                model: ApiList,
                key: "id",
            },
            onDelete: "CASCADE", // 父记录删除时自动删除子记录
        },
        fileUrls: {
            type: DataTypes.JSON,
            allowNull: true,
        },
        version: {
            type: DataTypes.STRING,
            allowNull: false,
        },
        status: {
            type: DataTypes.STRING,
            allowNull: false,
            defaultValue: "dev",
        },
        userId: {
            type: DataTypes.INTEGER,
            allowNull: false,
            references: {
                model: User,
                key: "id",
            },
        },
    },
    {
        timestamps: true, // 自动添加 createdAt 和 updatedAt 字段
    }
);

// 定义 ApiList 与 ApiListDetails 的关联
ApiList.hasMany(ApiListDetails, { foreignKey: "apiListId", as: "children" });
ApiListDetails.belongsTo(ApiList, { foreignKey: "apiListId" });

module.exports = ApiListDetails;
